const router = require('koa-router')();
const controllers = require('../../typings/src/controllers/index');
const middlewares = require('../../typings/src/middlewares/index');
router.prefix('/api');
//用户
router
    .post('/login', middlewares.resHandler(), controllers.user.login) //登录

    .post('/register', middlewares.resHandler(), controllers.user.register) //注册

    .get('/get/user/token', middlewares.resHandler(), middlewares.auth(), controllers.user.getUserToken) //通过token获取用户信息

    .get('/get/user', middlewares.resHandler(), controllers.user.getUser) //通过username获取用户信息


//文章
router
    .get('/get/article', middlewares.resHandler(), controllers.article.getArticle) //获取话题

    .get('/get/article/content', middlewares.resHandler(), controllers.article.getArticleContent) //获取话题详情

    .get('/get/unanswerd/topic', middlewares.resHandler(), controllers.article.getUnansweredTopic) //获取无人回复的话题

    .get('/get/user/other/topic', middlewares.resHandler(), controllers.article.getUserOtherTopic) //获取作者其他话题

    .get('/get/user/name/article', middlewares.resHandler(), controllers.article.getUserArticle) //获取用户名获取最近创建的话题

    .get('/get/user/name/replie', middlewares.resHandler(), controllers.article.getUserReplie) //获取用户名获取最近参与的话题


//分类
router
    .get('/get/categorie', middlewares.resHandler(), controllers.categorie.getCategorie) //获取分类


//上传文件
router
    .post('/upload/picture/token', middlewares.resHandler(), middlewares.auth(), controllers.uploadImg.uploadPicture)  //上传图片


//测试
router
    .get('/ceshi', middlewares.resHandler(), controllers.user.ceshi) //测试



module.exports = router;

/**
 *
 * @api {post} api/login 登录
 * @apiName oss-sts-apiName
 * @apiVersion  1.0.0
 *
 *
 * @apiParam  {String} name 用户名
 * @apiParam  {String} pass 用户密码
 *
 * @apiSuccess (200) {type} apiSuccess 成功
 *
 * @apiParamExample  {type} Request-Example:
 *
 * {
 *  pass:1
 *  name:1,
 * }
 *
 *
 * @apiSuccessExample {type} Success-Response:
 *{
 *  code: 200,
 *  message: '登录成功！',
 *  authorization,
 * }
 *
 *
 */

/**
* @api {方法} 路径 标题
* @apiGroup Group
* @apiDescription 描述这个API的信息
*
* @apiParam {String} userName 用户名
* @apiParamExample {json} request-example
* {
*  "userName": "Eve"
* }
*
* @apiError {String} message 错误信息
* @apiErrorExample  {json} error-example
* {
*   "message": "用户名不存在"
* }
*
*
* @apiSuccess {String} userName 用户名
* @apiSuccess {String} createTime 创建时间
* @apiSuccess {String} updateTime 更新时间
* @apiSuccessExample  {json} success-example
* {
*   "userName": "Eve",
*   "createTime": "1568901681"
*   "updateTime": "1568901681"
* }
*/


//路由配置
// const koaBody = require('koa-body');
// const mainController = require('../controllers/main');
// const authController = require('../controllers/auth');
// const routes = [
//     { method: 'get', url: '/', middlewares: [mainController.main] },
//     { method: 'post', url: '/auth/register', middlewares: [koaBody(), authController.register] },
// ];
// module.exports = { routes }

// const resHandler = require('./middlewares/res-handler');
// const { routes } = require('./routes');
// // 路由
// const router = new KoaRouter({ prefix: configs.router.prefix });
// // 绑定路由
// routes.map(route => {
//     router[route.method](route.url, resHandler(), route.middlewares);
// });
// app.use(router.routes());



// //数据库连接
// const mysql2 = require('mysql2/promise');
// const getUserService = require('../services/user');
// const getCategoryService = require('../services/category');
// const getArticleService = require('../services/article');
// const getReplyService = require('../services/reply');
// let db;
// let services;
// module.exports = (config) => {
//     return async (ctx, next) => {
//         if (!db) {
//             try {
//                 db = await mysql2.createConnection(config);
//                 services = {
//                     user: getUserService(db),
//                     category: getCategoryService(db),
//                     article: getArticleService(db),
//                     reply: getReplyService(db),
//                 }
//             } catch (err) {
//                 ctx.throw(500, { code: -1, message: '数据库链接失败', errors: err.toString() });
//             }
//         } if (db) {
//             ctx.state.db = db;
//             ctx.state.services = services;
//         } await next();
//     }
// }



// //鉴权
// // ./src/middlewares/auth.js
// const jsonwebtoken = require('jsonwebtoken');
// const config = require('../configs');

// module.exports = () => {

//     return async (ctx, next) => {
//         try {
//             let user = jsonwebtoken.verify(ctx.headers.authorization, config.auth.secretKey);
//             ctx.state.user = user;
//         } catch (err) {
//             ctx.throw(401, {
//                 code: -1,
//                 message: '你没有登录'
//             });
//         }

//         await next();
//     };
// }



// //错误处理
// // ./src/middlewares/res-handler.js
// module.exports = () => {
//     return async (ctx, next) => {
//         try {
//             await next();
//             ctx.body = {
//                 code: 0,
//                 message: '',
//                 results: ctx.body
//             }
//         } catch (err) {
//             ctx.status = err.status || 500;
//             ctx.body = {
//                 code: err.code,
//                 message: err.message,
//                 errors: err.errors
//             };
//             ctx.app.emit('error', err, ctx);
//         }
//     }
// }



// //服务
// module.exports = (db) => {
//     return {
//         getUserByUsername: async function (username) {
//             let user = await this.getProfile(username, 1);
//             return user;
//         },
//         getProfile: async function (value, type = 0) {
//         },
//     }
// } 